package com.hilon.segment.mybatis.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.ConfigAttribute;
import org.springframework.security.access.SecurityConfig;
import org.springframework.security.web.access.intercept.FilterInvocationSecurityMetadataSource;
import org.springframework.security.web.util.matcher.RequestMatcher;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class FilterInvocationSecurityMetadataSourceImpl implements FilterInvocationSecurityMetadataSource {

    private final Map<RequestMatcher, Collection<ConfigAttribute>> requestMatcherCollectionMap;

    @Autowired(required = false)
    public FilterInvocationSecurityMetadataSourceImpl(Map<RequestMatcher, Collection<ConfigAttribute>> requestMatcherCollectionMap) {
        this.requestMatcherCollectionMap = requestMatcherCollectionMap;
    }

    /**
     * 该方法为了判定用户请求的url是否在权限表中存在.
     * 此处假定请求的url都是存在的, 扔给了AccessDecisionManager 来处理这个问题.
     */
    @Override
    public Collection<ConfigAttribute> getAttributes(Object object) throws IllegalArgumentException {
        // TODO 这里后期可能需要做一下修改, 判定一下权限表里是不是有这个url.
        Collection<ConfigAttribute> co = new ArrayList<>();
        co.add(new SecurityConfig("null"));
        return co;
    }

    /**
     * 随便返回了个东西, 避免警告用的.
     * @return 随便申明的, 没啥用.
     */
    @Override
    public Collection<ConfigAttribute> getAllConfigAttributes() {
        Set<ConfigAttribute> allAttributes = new HashSet<>();
        this.requestMatcherCollectionMap.values().forEach(allAttributes::addAll);
        return allAttributes;
    }

    @Override
    public boolean supports(Class<?> clazz) {
        return true;
    }
}
